package pwd.allen.androiddemo.activity;

import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.View;
import android.widget.*;
import androidx.appcompat.app.AppCompatActivity;

import pwd.allen.androiddemo.R;
import pwd.allen.androiddemo.util.MethodCommon;
import pwd.allen.androiddemo.util.ToastUtil;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * ListView
 * 启动类，列举项目的activity供跳转
 */
public class ListViewActivity extends AppCompatActivity implements AdapterView.OnItemClickListener, CompoundButton.OnCheckedChangeListener, AdapterView.OnItemLongClickListener {

    /**
     * 自定义的activity集合
     */
    private static List<Class> classes = new ArrayList<>();
    private ListView lv;

    private CheckBox ck_diviver;
    private CheckBox ck_selector;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        try {
            PackageManager packageManager = getPackageManager();
            PackageInfo packageInfo = packageManager.getPackageInfo(
                    getPackageName(), PackageManager.GET_ACTIVITIES);
            ActivityInfo[] activities = packageInfo.activities;
            for (ActivityInfo activity :activities) {
                if (!activity.name.equals(this.getClass().getName())) {
                    Class<?> aClass = Class.forName(activity.name);
                    classes.add(aClass);
                }
            }
        } catch (PackageManager.NameNotFoundException | ClassNotFoundException e) {
            e.printStackTrace();
        }
        classes.sort((a, b) -> a.getSimpleName().compareTo(b.getSimpleName()));
        List<String> activitiesName = classes.stream().map(clazz -> clazz.getSimpleName()).collect(Collectors.toList());

        lv = findViewById(R.id.lv);
        // 声明一个下拉列表的数组适配器
        ArrayAdapter<String> startAdapter = new ArrayAdapter<>(this, R.layout.item_select, activitiesName);
        // setAdapter后才有数据项
        lv.setAdapter(startAdapter);
        Log.i("pwd", String.format("数据项有：%d个", lv.getCount()));
        // 给下拉框设置选择监听器，一旦用户选中某一项，就触发监听器的onItemSelected方法
        lv.setOnItemClickListener(this);

        lv.setOnItemLongClickListener(this);

        // CheckBox用于处理是否显示分隔符
        ck_diviver = findViewById(R.id.ck_divider);
        ck_diviver.setChecked(true);
        ck_diviver.setOnCheckedChangeListener(this);
        // CheckBox用于处理是否显示按压背景
        ck_selector = findViewById(R.id.ck_selector);
        ck_selector.setOnCheckedChangeListener(this);
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Intent intent = new Intent(this, classes.get(position));
        startActivity(intent);
    }

    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        switch (buttonView.getId()) {
            case R.id.ck_divider:
                // 显示分隔线
                if (ck_diviver.isChecked()) {
                    // 从资源文件获得图形对象
                    Drawable drawable = getResources().getDrawable(R.color.black, getTheme());
                    lv.setDivider(drawable);
                    // 设置列表视图的分隔线高度
                    lv.setDividerHeight(MethodCommon.dip2px(this, 1));
                } else {
                    lv.setDivider(null);
                    lv.setDividerHeight(0);
                }
                break;

            case R.id.ck_selector:
                // 显示按压背景
                if (ck_selector.isChecked()) {
                    // 设置列表项的按压状态图形
                    lv.setSelector(R.drawable.list_selector);
                } else {
                    Drawable drawable = getResources().getDrawable(R.color.transparent, getTheme());
                    lv.setSelector(drawable);
                }
                break;
        }
    }

    @Override
    public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
        ToastUtil.show(this, String.format("你长按了%s", classes.get(position).getSimpleName()));
        // 返回false的话放开按压时会触发点击事件
        return true;
    }
}
